const { createClient } = require('redis')

/**
 * redis 实例化
 */
const redisClient = async () => {
  const client = createClient();
  client.on('error', (err) => console.log('Redis Client Error', err));
  await client.connect();
  return client;
}

/**
 * 存入数组或对象
 */
const setObject = async (key, value) => {
  const client = await redisClient()
  value = JSON.stringify(value)
  await client.set(key, value);
}

/**
 * 读取数组或对象
 * @param key
 * @returns {Promise<any>}
 */
const getObject = async (key) => {
  const client = await redisClient()
  const value = await client.get(key);
  return JSON.parse(value)
}

module.exports = { redisClient, setObject, getObject }
